package com.biyesheji.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.biyesheji.domain.Business;
import com.biyesheji.domain.Orders;
import com.biyesheji.domain.Wares;
import com.biyesheji.dao.WaresMapper;
import com.biyesheji.service.IWaresService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.biyesheji.utils.OrdersTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.util.List;

/**
 * <p>
 * 商品管理 服务实现类
 * </p>
 *
 * @author 崔
 * @since 2023-02-12
 */
@Service
public class WaresServiceImpl extends ServiceImpl<WaresMapper, Wares> implements IWaresService {


    @Autowired
    OrdersServiceImpl ordersService;


    @Override
    public List<Wares> getWares(Long id, String waresName,  String maxLng, String minLng, String maxLat, String minLat, String address, Long createPhone, Integer status) {
        LambdaQueryWrapper<Wares> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(id != null && id != -1,Wares::getId, id);
        queryWrapper.like(waresName != null,Wares::getWaresName, waresName);
        queryWrapper.like(address != null,Wares::getAddress, address);

        queryWrapper.le(maxLng != null, Wares::getAddressX, maxLng);
        queryWrapper.ge(minLng != null, Wares::getAddressX, minLng);
        queryWrapper.le(maxLat != null, Wares::getAddressY, maxLat);
        queryWrapper.ge(minLat != null, Wares::getAddressY, minLat);

        queryWrapper.like(createPhone != null && createPhone != -1,Wares::getCreatePhone, createPhone);
        queryWrapper.eq(status != null,Wares::getStatus, status);
        queryWrapper.orderByDesc(Wares::getUpdateTime);
        List<Wares> waress = this.list(queryWrapper);


        return waress;
    }

    @Override
    public List<Wares> getWares2(Long id, String waresName,  String address, Long createPhone, Integer status) {
        LambdaQueryWrapper<Wares> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(id != null,Wares::getId, id);
        queryWrapper.like(waresName != null,Wares::getWaresName, waresName);
        queryWrapper.like(address != null,Wares::getAddress, address);


        queryWrapper.like(createPhone != null,Wares::getCreatePhone, createPhone);
        queryWrapper.eq(status != null,Wares::getStatus, status);
        queryWrapper.orderByDesc(Wares::getUpdateTime);

        List<Wares> waress = this.list(queryWrapper);


        return waress;
    }

    @Override
    public List<Wares> getWares3(Long id, String waresName,  String maxLng, String minLng, String maxLat, String minLat, String address, Long createPhone, Integer status) {
        LambdaQueryWrapper<Wares> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(id != null,Wares::getId, id);
        queryWrapper.like(waresName != null,Wares::getWaresName, waresName);
        queryWrapper.like(address != null,Wares::getAddress, address);

        queryWrapper.le(maxLng != null, Wares::getAddressX, maxLng);
        queryWrapper.ge(minLng != null, Wares::getAddressX, minLng);
        queryWrapper.le(maxLat != null, Wares::getAddressY, maxLat);
        queryWrapper.ge(minLat != null, Wares::getAddressY, minLat);

        queryWrapper.eq(createPhone != null,Wares::getCreatePhone, createPhone);
        queryWrapper.eq(status != null,Wares::getStatus, status);
        queryWrapper.orderByDesc(Wares::getUpdateTime);
        List<Wares> waress = this.list(queryWrapper);


        return waress;
    }

    @Override
    public Boolean updateWaresStatus(Wares wares) throws ParseException {

        if (wares.getId() == null || wares.getStatus() == null){
            return false;
        }
        if (wares.getStatus() == 0 ){

            System.out.println("这里运行了");
            ordersService.updateStatusByWaresId(wares);
        }

        if (wares.getStatus() == 3){

            LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Orders::getWaresId,wares.getId());
            Orders orders = new Orders();
            orders.setStatus(0);
            ordersService.update(orders, queryWrapper);

        }


        this.updateById(wares);

        return true;
    }

    @Override
    public List<Wares> getWaresByBusinessPhone(String businessPhone) {

        LambdaQueryWrapper<Wares> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq( businessPhone != null && !businessPhone.equals("-1"),Wares :: getCreatePhone, businessPhone);
        queryWrapper.orderByDesc(Wares::getUpdateTime);
        List<Wares> list = this.list(queryWrapper);

        return list;
    }


}
